package com.esunrising.esungsc.service.impl;

import com.esunrising.esungsc.dao.FIGLAccDocAssistanceDao;
import com.esunrising.esungsc.entity.FIGLAccDocAssistance;
import com.esunrising.esungsc.service.FIGLAccDocAssistanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class FIGLAccDocAssistanceServiceImpl extends BaseServiceImpl<FIGLAccDocAssistanceDao, FIGLAccDocAssistance> implements FIGLAccDocAssistanceService {

    @Autowired
    private FIGLAccDocAssistanceDao figlAccDocAssistanceDao;

    /**
     * 删除已有GSC账务凭证辅助原始数据，再保存
     *
     * @param accDocAssistance 需要保存的GSC账务凭证分组数原始
     */
    @Override
    public void syncSave(FIGLAccDocAssistance accDocAssistance) {

        // 如果存在，则先删除
        Optional<FIGLAccDocAssistance> byId = figlAccDocAssistanceDao.findById(accDocAssistance.getID());
        byId.ifPresent( doc -> {
            figlAccDocAssistanceDao.deleteById(doc.getID());
        });
        // 保存数据
        super.save(accDocAssistance);
    }

    /**
     * GSC账务凭辅助原始数组存入数据库，其中ID重复的会覆盖
     * @param accDocAssList GSC账务凭证辅助原始数组
     */
    @Override
    public void syncAccDocAssList(List<FIGLAccDocAssistance> accDocAssList){
        for (FIGLAccDocAssistance accDocAssistance : accDocAssList) {
            syncSave(accDocAssistance);
        }
    }
}
